COMMAND="$1"

# 按照日期或者作者统计代码量
function stats()
{
	start_date="$1"
	end_data="$2"
	author="$3"
	if [[ $author == '' ]];then
		git log --since="$start_date" --before="$end_data" --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
	elif [[ $start_date != '' && $end_data != '' ]];then
		
		git log --since="$start_date" --before="$end_data" --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
		
	fi
	#git log --since="$1" --before="$2" --author="$3" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s ", add, subs, loc }'
}

# 统计所有代码量
function all()
{
	git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
}

function topn()
{
	git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n "$1"
}

function showhelp()
{
	echo "======================使用说明============================="
	echo "按照日期统计特定作者 gitstats.sh stats 开始日期 结束日期 author"
	echo "统计所有 gitstats.sh all"
	echo "查看仓库提交者前几名 gitstats.sh  top 10" 
	echo "======================使用说明============================="
	exit 0
}

function contribution()
{
	git log --pretty='%aN' | sort -u | wc -l
}


if [[ "$COMMAND" == "stats" ]]; then
	stats $2 $3 $4
elif [[ "$COMMAND" == "all" ]]; then
    all
elif [[ "$COMMAND" == "top" ]]; then
    topn $2
else
	showhelp
fi






## git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done

